package com.lc.cehui.ui.activity.mine;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.blankj.utilcode.util.LogUtils;
import com.bumptech.glide.load.MultiTransformation;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.hjq.bar.OnTitleBarListener;
import com.hjq.bar.TitleBar;
import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallback;
import com.hjq.http.listener.OnHttpListener;
import com.lc.cehui.R;
import com.lc.cehui.app.AppActivity;
import com.lc.cehui.http.glide.GlideApp;
import com.lc.cehui.http.request.StaffAddApi;
import com.lc.cehui.http.request.StaffDelApi;
import com.lc.cehui.http.request.UpdateImageApi;
import com.lc.cehui.http.response.RegisterBean;
import com.lc.cehui.http.response.UpdateImageBean;
import com.lc.cehui.other.IntentKey;
import com.lc.cehui.util.CourseUtils;
import com.lc.cehui.util.SPUtil;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.entity.LocalMedia;

import java.io.File;
import java.util.List;

import androidx.appcompat.widget.AppCompatImageView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class CreateStaffActivity extends AppActivity {
    @BindView(R.id.et_name)
    EditText etName;
    @BindView(R.id.et_number)
    EditText etNumber;
    @BindView(R.id.tv_job)
    EditText tvJob;
    @BindView(R.id.et_password)
    EditText etPassword;
    @BindView(R.id.bt_submit)
    Button btSubmit;
    @BindView(R.id.iv_person_data_avatar)
    AppCompatImageView ivPersonDataAvatar;
    @BindView(R.id.titleBar)
    TitleBar titleBar;
    private String picurl;
    private String id = "";

    @Override
    protected int getLayoutId() {
        return R.layout.activity_createstaff;
    }

    @Override
    protected void initView() {
        titleBar.setOnTitleBarListener(new OnTitleBarListener() {
            @Override
            public void onLeftClick(View v) {
finish();
            }

            @Override
            public void onTitleClick(View v) {

            }

            @Override
            public void onRightClick(View v) {
                my_staff_del();
            }
        });
        if (CourseUtils.isNotNullOrEmpty(getIntent().getStringExtra("type"))) {
            etName.setText(getIntent().getStringExtra("name"));
            etNumber.setText(getIntent().getStringExtra("mobile"));
            tvJob.setText(getIntent().getStringExtra("job"));
            etPassword.setText(getIntent().getStringExtra("password"));
            GlideApp.with(getActivity())
                    .load(getIntent().getStringExtra("picurl"))
                    .placeholder(R.mipmap.mine_default_avatar)
                    .error(R.mipmap.mine_default_avatar)
                    .transform(new MultiTransformation<>(new CenterCrop(), new CircleCrop()))
                    .into(ivPersonDataAvatar);
            etName.setText(getIntent().getStringExtra("name"));
            id = getIntent().getStringExtra("id");
            picurl = getIntent().getStringExtra("picurl");
            titleBar.getRightView().setVisibility(View.VISIBLE);
        }else {
            titleBar.getRightView().setVisibility(View.GONE);
        }

    }

    private void my_staff_del() {
        EasyHttp.post(this)
                .api(new StaffDelApi()
                        .setId(id).setUserid(SPUtil.get(this,IntentKey.USERID,0)))
                .request(new HttpCallback<RegisterBean>(this) {

                    @Override
                    public void onSucceed(RegisterBean data) {
                        toast(data.getMsg());
                        if (data.getCode().equals("200")) {
                            finish();
                        }

                    }
                });
    }

    private void my_staff_add() {
        EasyHttp.post(this)
                .api(new StaffAddApi()
                        .setMobile(etNumber.getText().toString()).setNames(etName.getText().toString()).setPosition(tvJob.getText().toString()).setUserid(SPUtil.get(this, IntentKey.USERID, 0)).setPassword(etPassword.getText().toString()).setPicurl(picurl).setId(id))
                .request(new HttpCallback<RegisterBean>(this) {

                    @Override
                    public void onSucceed(RegisterBean data) {
                        toast(data.getMsg());
                        if (data.getCode().equals("200")) {
                            finish();
                        }

                    }
                });
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }


    @OnClick({R.id.iv_person_data_avatar, R.id.bt_submit})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_person_data_avatar:
                showAlbum(PictureConfig.CHOOSE_REQUEST);
                break;
            case R.id.bt_submit:

                my_staff_add();
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case PictureConfig.CHOOSE_REQUEST:
                    // 结果回调
                    List<LocalMedia> result = PictureSelector.obtainMultipleResult(data);
                    LogUtils.e(result);
                    insertImg(new File(result.get(0).getCutPath()));
                    break;
                default:
                    break;
            }
        }
    }

    private void insertImg(File cutPath) {
        EasyHttp.post(this)
                .api(new UpdateImageApi()
                        .setImg(cutPath))
                .request(new OnHttpListener<UpdateImageBean>() {

                    @Override
                    public void onSucceed(UpdateImageBean data) {
//
                        picurl = data.getData().getImg();

                        GlideApp.with(getActivity())
                                .load(IntentKey.PAGEURL + data.getData().getImg())
                                .placeholder(R.mipmap.mine_default_avatar)
                                .error(R.mipmap.mine_default_avatar)
                                .transform(new MultiTransformation<>(new CenterCrop(), new CircleCrop()))
                                .into(ivPersonDataAvatar);
                    }

                    @Override
                    public void onFail(Exception e) {

                    }
                });
    }
}
